package com.example.exampleviewpager.ui.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.exampleviewpager.R;
import com.example.exampleviewpager.ui.activity.MainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MyPagerAdapter
 * @Description TODO
 * @Author ${孙伟豪}
 * @Date 2020/12/22 18:32
 * @Version 1.0
 */
public class MyPagerAdapter extends PagerAdapter {
    private  Activity activity;
    private ArrayList<View> arrayList;
    private String[] titles;
    private int[] images;

    public MyPagerAdapter(Activity activity) {
        this.activity=activity;
    }
    //页数
    @Override
    public int getCount() {
        return arrayList==null?0:arrayList.size();
    }
    //view是否属于object
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    private static final String TAG = "MyPagerAdapter";
    //创建的view
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        Log.d(TAG, "instantiateItem: "+position);
        View view=arrayList.get(position);
        TextView tv=view.findViewById(R.id.tv);
        tv.setText(titles[position]);
        ImageView imageView=view.findViewById(R.id.iv);
        imageView.setImageResource(images[position]);
        container.addView(view);
        return view;
    }

    /**
     * 销毁的view
     * @param container
     * @param position
     * @param object： 表示之前添加的View
     */
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        Log.d(TAG, "destroyItem: "+position);
        View view= (View) object;
        container.removeView(view);
    }

    public void setData(ArrayList<View> arrayList, String[] titles, int[] images) {
        this.arrayList=arrayList;
        this.titles=titles;
        this.images=images;

    }
}
